home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / gle / util / surf / memory.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-29  |  2.4 KB  |  141 lines

  1. #include "all.h"
  2. #ifndef __TURBOC__
  3. #define farcalloc calloc
  4. #else
  5. #include <alloc.h>
  6. #endif
  7. extern char errgle[];
  8. int free_savemem(void);
  9. char *sdup(char *s)
  10. {
  11.     char *v;
  12.     v = myalloc(strlen(s)+1);
  13.     strcpy(v,s);
  14.     return v;
  15. }
  16. char *save_memory;
  17. free_savemem()
  18. {
  19.     if (save_memory!=NULL) free(save_memory);
  20.     save_memory = 0;
  21. }
  22. init_memory()
  23. {
  24.     save_memory = malloc(1000);
  25.     if (save_memory == NULL) gle_abort("Not able to alocate save memory\n");
  26. }
  27. static long totalmem,worst;
  28. void myfree(void *p)
  29. {
  30.     myfrees(p,"UNKNOWN");
  31. }
  32. void myfrees(void *p,char *s)
  33. {
  34.     static long *l;
  35.     l = p;
  36.     totalmem -= *(--l);
  37.     if (*(--l)!=1234) {
  38.         sprintf(errgle,"Free memory (%s) not mine %ld  %ld \n",s,*l,*(++l));
  39.         gle_abort(errgle);
  40.     }
  41.     free(l);
  42.     return;
  43. }
  44. void *myallocn(long nitems,long size)
  45. {
  46.     return myallocz(nitems*size);
  47. }
  48. void *myalloc(long size)
  49. {
  50.     static void *p;
  51.     static long *l;
  52.     if (size==0) {
  53.         sprintf(errgle," error, attempt to allocate ZERO memory \n");
  54.         gle_abort(errgle);
  55.     }
  56.     if (size>40000) gprint(" allocating alot of memory %d \n",size);
  57.     p = malloc(size+8);
  58.     if (p==NULL) {
  59.         freeafont();
  60.         p = malloc(size+8);
  61.         if (p==NULL) {
  62.          free_savemem();
  63.          sprintf(errgle,"\n Memory allocation failure (size %d)  \n"
  64.             ,size);
  65.          gle_abort(errgle);
  66.         }
  67.     }
  68.     l = p;
  69.     *l++ = 1234;
  70.     *l = size+8;
  71.     totalmem += *l;
  72.     if (totalmem>worst) worst = totalmem;
  73.     p = ++l;
  74.     return p;
  75. }
  76. void *myallocz(long size)
  77. {
  78.     static void *p;
  79.     static long *l;
  80.     if (size==0) {
  81.         free_savemem();
  82.         sprintf(errgle," zerror, attempt to allocate ZERO memory \n");
  83.         gle_abort(errgle);
  84.     }
  85.     if (size>40000) {
  86.       gprint(" zallocating alot of memory %ld \n",size);
  87.     }
  88.     p = farcalloc(1,size+8);
  89.     if (p==NULL) {
  90.        freeafont();
  91.        p = farcalloc(1,size+8);
  92. /*        if (p==NULL) {
  93.         free_cache();
  94.         gprint("Freeing cached characters\n");
  95.         p = farcalloc(1,size+8);
  96.        }
  97. */
  98.        if (p==NULL) {
  99.         free_savemem();
  100.         sprintf(errgle,"\n\n zMemory allocation failure %d  \n\n"
  101.             ,size);
  102.         gle_abort(errgle);
  103.        }
  104.     }
  105.     l = p;
  106.     *l++ = 1234;
  107.     *l = size+8;
  108.     totalmem += *l;
  109.     if (totalmem>worst) worst = totalmem;
  110.     p = ++l;
  111.     return p;
  112. }
  113. long mem_total()
  114. {
  115.     return totalmem;
  116. }
  117. long mem_worst()
  118. {
  119.     return worst;
  120. }
  121.  
  122. long gtotalmem;
  123. /*
  124. #include <alloc.h>
  125. void far * far _graphgetmem(unsigned size)
  126. {
  127.     gtotalmem += size;
  128.     totalmem += size;
  129.     return(farmalloc(size));
  130. }
  131. void far _graphfreemem(void far *ptr, unsigned size)
  132. {
  133.     totalmem -= size;
  134.     gtotalmem -= size;
  135.     farfree(ptr);
  136. }
  137.  
  138.  
  139. */
  140.  
  141.